﻿Imports System.Data.SqlClient
Public Class frmQuanLyChuNha

    Private Sub frmQuanLyChuNha_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim listNguoiDang As New List(Of NguoiDangDTO)
        listNguoiDang = NguoiDangBUS.GetAll()
        gvDanhSachCN.DataSource = listNguoiDang
        gvDanhSachCN.AutoGenerateColumns = False

        btnLuuLai.Enabled = False
    End Sub

    Private Sub gvDanhSachCN_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvDanhSachCN.SelectionChanged
        txtMaCN.Text = gvDanhSachCN.CurrentRow.Cells(0).Value
        txtTenCN.Text = gvDanhSachCN.CurrentRow.Cells(1).Value
        txtDiaChiCN.Text = gvDanhSachCN.CurrentRow.Cells(2).Value
        txtSoDT.Text = gvDanhSachCN.CurrentRow.Cells(3).Value
        txtEmail.Text = gvDanhSachCN.CurrentRow.Cells(4).Value
    End Sub

    Private Sub btnThemCN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemCN.Click
        Dim macn As String = ""
        Dim macuoi As String = NguoiDangBUS.LayMa_ND_Cuoi()
        macn = NguoiDangBUS.TinhMaNguoiDang(macuoi)

        txtMaCN.Text = macn

        txtTenCN.Text = ""
        txtDiaChiCN.Text = ""
        txtSoDT.Text = ""
        txtEmail.Text = ""

        btnLuuLai.Enabled = True
        btnXoaCN.Enabled = False
        btnSuaNguoiDang.Enabled = False
    End Sub

    Private Sub btnLuuLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuuLai.Click
        Dim nguoidang As New NguoiDangDTO()
        If NguoiDangBUS.KiemTraDayDu(txtTenCN.Text, txtDiaChiCN.Text, txtEmail.Text, txtSoDT.Text) Then
            nguoidang.strMaNguoiDang = txtMaCN.Text
            nguoidang.strHoTen = txtTenCN.Text
            nguoidang.strDiaChi = txtDiaChiCN.Text
            nguoidang.strEmail = txtEmail.Text
            nguoidang.strDienThoai = txtSoDT.Text
            nguoidang.isTamXoa = False

            NguoiDangBUS.Insert(nguoidang)

            MessageBox.Show("Thông tin đã được lưu lại thành công!", "THÔNG BÁO")

            Dim listNguoiDang As New List(Of NguoiDangDTO)
            listNguoiDang = NguoiDangBUS.GetAll()
            gvDanhSachCN.DataSource = listNguoiDang
            gvDanhSachCN.AutoGenerateColumns = False

        Else
            MessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "THÔNG BÁO")
        End If

        btnLuuLai.Enabled = False
        btnXoaCN.Enabled = True
        btnSuaNguoiDang.Enabled = True

    End Sub

    Private Sub btnXoaCN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaCN.Click
        If IsNothing(gvDanhSachCN.SelectedRows) Then
            MessageBox.Show("Bạn chưa chọn dòng nào để Xóa!!", "THÔNG BÁO")
        Else
            Dim idMaND As String = txtMaCN.Text
            Dim istamxoa As Boolean = True

            NguoiDangBUS.Update_TamXoa(idMaND, istamxoa)

            MessageBox.Show("Dữ liệu đã được đánh dấu Xóa!!", "THÔNG BÁO")

            Dim listNguoiDang As New List(Of NguoiDangDTO)
            listNguoiDang = NguoiDangBUS.GetAll()
            gvDanhSachCN.DataSource = listNguoiDang
            gvDanhSachCN.AutoGenerateColumns = False

        End If
    End Sub

    
    Private Sub btnSuaNguoiDang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuaNguoiDang.Click
        Dim nguoidang As New NguoiDangDTO()
        If NguoiDangBUS.KiemTraDayDu(txtTenCN.Text, txtDiaChiCN.Text, txtEmail.Text, txtSoDT.Text) Then
            nguoidang.strMaNguoiDang = txtMaCN.Text
            nguoidang.strHoTen = txtTenCN.Text
            nguoidang.strDiaChi = txtDiaChiCN.Text
            nguoidang.strEmail = txtEmail.Text
            nguoidang.strDienThoai = txtSoDT.Text
            nguoidang.isTamXoa = False

            NguoiDangBUS.Update(nguoidang)

            MessageBox.Show("Bạn đã Sửa thành công !!", "THÔNG BÁO")

            Dim listNguoiDang As New List(Of NguoiDangDTO)
            listNguoiDang = NguoiDangBUS.GetAll()
            gvDanhSachCN.DataSource = listNguoiDang
            gvDanhSachCN.AutoGenerateColumns = False
        Else
            MessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "THÔNG BÁO")
        End If
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        If rbtTimMaCN.Checked = True Then
            If Not txtThongTinTim.Text = "" Then
                Dim mand As String = txtThongTinTim.Text
                Dim list As New List(Of NguoiDangDTO)
                list = NguoiDangBUS.TimKiemTheoMa(mand)
                If list.Count = 0 Then
                    MessageBox.Show("Xin lỗi! Không tìm thấy thông tin cần tìm !!", "THÔNG BÁO")
                Else
                    gvDanhSachCN.DataSource = list
                End If

            Else
                MessageBox.Show("Bạn Chưa nhập thông tin để Tìm!!", "THÔNG BÁO")
            End If
        ElseIf rbtHoTen.Checked = True Then
            If Not txtThongTinTim.Text = "" Then
                Dim ten As String = txtThongTinTim.Text

                Dim list As New List(Of NguoiDangDTO)
                list = NguoiDangBUS.TimKiemTheTen(ten)
                If list.Count = 0 Then
                    MessageBox.Show("Xin lỗi! Không tìm thấy thông tin cần tìm !!", "THÔNG BÁO")
                Else
                    gvDanhSachCN.DataSource = list
                End If
            Else
                MessageBox.Show("Bạn Chưa nhập thông tin để Tìm!!", "THÔNG BÁO")
            End If
        End If
    End Sub

    Private Sub btnTatCa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTatCa.Click
        Dim listNguoiDang As New List(Of NguoiDangDTO)
        listNguoiDang = NguoiDangBUS.GetAll()
        gvDanhSachCN.DataSource = listNguoiDang
        gvDanhSachCN.AutoGenerateColumns = False
    End Sub
End Class